package com.hrb.douban.activtity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.util.Log;
import android.view.MenuItem;
import android.widget.FrameLayout;

import com.hrb.douban.R;
import com.hrb.douban.activtity.base.BaseActivity;
import com.hrb.douban.fragment.ComingFragment;
import com.hrb.douban.fragment.MineFragment;
import com.hrb.douban.fragment.PopularFragment;
import com.hrb.douban.fragment.TopFragment;
import com.hrb.douban.utils.BottomNavigationViewHelper;
import com.hrb.douban.utils.ToastUtil;


import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {

    @BindView(R.id.content_fragment)
    FrameLayout contentFragment;
    @BindView(R.id.design_navigation_view)
    BottomNavigationView designNavigationView;
    private ActionBar actionBar;
    private PopularFragment popularFragment;
    private ComingFragment comingFragment;
    private TopFragment topFragment;
    private MineFragment mineFragment;
    private Fragment[] fragments;
    private int lastFragment;//记录选中的fragment；

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ToastUtil.init(getApplication());
        ButterKnife.bind(this);
        actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            //actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
            actionBar.hide();
        }
        /**底部菜单栏按图片切换*/
        designNavigationView.setItemIconTintList(null);
        designNavigationView.setOnNavigationItemSelectedListener(monNavigationItemSelectedListener);
        BottomNavigationViewHelper.disableShiftMode(designNavigationView);
        initFragment();
    }
    /**初始化fragment部分*/
    private void initFragment(){
        popularFragment=new PopularFragment();
        comingFragment=new ComingFragment();
        topFragment =new TopFragment();
        mineFragment=new MineFragment();

        fragments=new Fragment[]{popularFragment,comingFragment,topFragment,mineFragment};
        getSupportFragmentManager().beginTransaction().replace(R.id.content_fragment,popularFragment).show(popularFragment).commit();
        //设置默认选中项
        designNavigationView.setSelectedItemId(R.id.menu_popular);
    }
    private BottomNavigationView.OnNavigationItemSelectedListener monNavigationItemSelectedListener=
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                    switch (menuItem.getItemId()){
                        case R.id.menu_popular:
                        {
                            if(lastFragment!=0){
                                switchFragment(lastFragment,0);
                                lastFragment=0;
                            }
                            return true;
                        }
                        case R.id.menu_coming:
                        {
                            if(lastFragment!=1){
                                switchFragment(lastFragment,1);
                                lastFragment=1;
                            }
//                            ToastUtil.show("2");
                            return true;
                        }
                        case R.id.menu_top:
                        {
                            if(lastFragment!=2){
                                switchFragment(lastFragment,2);
                                lastFragment=2;
                            }
//                            ToastUtil.show("3");
                            return true;
                        }
                        case R.id.menu_mine:
                            if(lastFragment!=3){
                                switchFragment(lastFragment,3);
                                lastFragment=3;
                            }
//                            ToastUtil.show("4");
                            return true;

                    }
                    return false;
                }
            };
    /**
     *
     * @param lastFragment 上次的fragment序号
     * @param index 要切换的fragme
     */
    private void switchFragment(int lastFragment,int index){
        FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
        transaction.hide(fragments[lastFragment]);
        if(fragments[index].isAdded()==false){
            transaction.add(R.id.content_fragment,fragments[index]);
        }
        transaction.show(fragments[index]).commitAllowingStateLoss();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //把结果传回到第一个fragment中
        fragments[0].onActivityResult(requestCode, resultCode, data);
        Log.d("mainactivity "+requestCode,resultCode+"  "+data);
    }
}
